﻿@{
    ViewBag.Title = "Регистрация пользователя";
}
@model AllAgreed.DataLayer.Models.User

<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<div class="center">
    @using (Html.BeginForm("Save", "User", FormMethod.Post))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <div class="editor-label">
                @Html.LabelFor(model => model.Login, "Логин", new { @class = "muted" })
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Login, new { @placeholder = "Ваш уникальный логин..." })
                @Html.LabelFor(model => model.Login, "Пользователь с таким логином уже зарегистрирован", new {id="LoginValidateMessage", @class = "text-error hidden" })
                @Html.ValidationMessageFor(model => model.Login)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Password, "Пароль", new { @class = "muted" })
            </div>
            <div class="editor-field">
                @Html.PasswordFor(model => model.Password, new { @placeholder = "Ваш пароль..." })
                @Html.ValidationMessageFor(model => model.Password)
            </div>
            <div class="editor-label">
                @Html.LabelFor(model => model.Email, "Почта", new { @class = "muted" })
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(model => model.Email, new { @placeholder = "Ваша электронная почта..." })
                @Html.ValidationMessageFor(model => model.Email)
            </div>
            <p>
                <input class="btn btn-primary" type="submit" value="Зарегистрироваться" />
            </p>
        </fieldset>
    }
</div>
@section Scripts
{
    <script type="text/javascript">
        $(document).ready(function () {
            var login = $("#Login");
            var valLogin = $("#LoginValidateMessage");
            login.on("keyup", function () {
                if ($(this).val().length > 3) {
                    $.ajax({
                        type: "POST",
                        url: "\Validate",
                        data: { login: $(this).val() },
                        cache: false,
                        success: function (data) {
                            if (data == true) {
                                login.addClass("error-login");
                                valLogin.removeClass("hidden");
                            } else {
                                login.removeClass("error-login");
                                valLogin.addClass("hidden");
                            }
                        }
                    });
                }
            });
        });
    </script>
}
